package com.gdyx.app.web.filter;

import com.gdyx.app.web.model.MpsUser;
import com.gdyx.app.web.svc.MpsOrgSvc;
import com.gdyx.app.web.svc.MpsOrgUserSvc;
import com.gdyx.app.web.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 使用注解标注过滤器
 *
 * @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器 属性filterName声明过滤器的名称, 可选
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
 * Created by Y_Coffee on 2017/7/18.
 */


//@WebFilter(filterName = "myFilter", urlPatterns = "/*")
public class AuthsFilter extends BaseController implements Filter {
    @Autowired
    MpsOrgUserSvc mpsOrgUserSvc;
    @Autowired
    MpsOrgSvc mpsOrgSvc;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse rep = (HttpServletResponse) servletResponse;

        String webPath = req.getContextPath();
        String uri = req.getRequestURI();

        HttpSession httpSession = req.getSession();

        //取消静态资源拦截
        if (uri.contains("/bootstrap/") || uri.contains("/plugins/") || uri.contains("/AdminLTE/") || uri.contains("/images/") || uri.contains("/js/") || uri.contains("/css/") || uri.contains("/editor/")) {
            filterChain.doFilter(req, rep);
        }
        //取消拦截
        if (uri.equals(webPath + "/") || uri.equals(webPath + "/logout") || uri.equals(webPath + "/doLogin" )|| uri.equals(webPath + "/goIndex" )) {
            filterChain.doFilter(req, rep);
        }else {
            MpsUser user = (MpsUser) httpSession.getAttribute("ADMIN_SESSION");
            String org_session = (String)httpSession.getAttribute("Org_SESSION");
            if (org_session==null){

            }
        }
    }

    @Override
    public void destroy() {

    }
}
